package com.example.user.myhjjf_app.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.user.myhjjf_app.HJJF_Url;
import com.example.user.myhjjf_app.R;
import com.example.user.myhjjf_app.model.Model_Banka;
import com.example.user.myhjjf_app.model.Model_Content;
import com.example.user.myhjjf_app.model.Model_Regional;
import com.example.user.myhjjf_app.model.Model_dk;
import com.example.user.myhjjf_app.presenter.Presenter_Cintent_adapter;
import com.example.user.myhjjf_app.presenter.Presenter_Dk_adapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.adapter.abslistview.MultiItemTypeAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Activity_bklist extends AppCompatActivity {

    @BindView(R.id.btn_back)
    LinearLayout btnBack;
    @BindView(R.id.tx_title)
    TextView txTitle;
    @BindView(R.id.btn_add)
    LinearLayout btnAdd;
    @BindView(R.id.btn_menu2)
    LinearLayout btnMenu2;
    @BindView(R.id.list_pt_item)
    ListView listPtItem;
    Model_dk modedk;
    List<Model_dk.ValueBean> dk;
    MultiItemTypeAdapter adapter;
    String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bklist);
        ButterKnife.bind(this);
        Bundle bundle = getIntent().getExtras();
        id = bundle.getString("id");
        init();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==1){
            init();
        }
    }


    @OnClick({R.id.btn_back, R.id.btn_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                break;
            case R.id.btn_add:
                Intent intent = new Intent(Activity_bklist.this,Activity_DkAdd.class);
                Bundle bundle = new Bundle();
                bundle.putString("type","add");
                bundle.putString("id", id);
                intent.putExtras(bundle);
                startActivityForResult(intent,0);
                break;
        }
    }

    private void init(){
        dk = new ArrayList<>();
        OkGo.<String>post(HJJF_Url.getHJJF_Url().login("api/site/port_list.do"))
                .tag(this)
                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        modedk = JSON.parseObject(response.body(), Model_dk.class);
                        for (int i = 0; i < modedk.getValue().size(); i++) {

                            dk.add(modedk.getValue().get(i));
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
        adapter = new MultiItemTypeAdapter(this, dk);
        adapter.addItemViewDelegate(new Presenter_Dk_adapter());
        listPtItem.setAdapter(adapter);
    }
}
